package com.share.shareplatform.sharegoods.config;

import com.share.shareplatform.util.Constants;
import com.share.shareplatform.util.StringUtils;
import com.share.shareplatform.util.TokenUtil;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
/*
*
* 在服务调用之前将token放入请求头中
* */
@Configuration
public class FeignConfig  implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        //path表示一个uri
        String path = requestTemplate.path();

        String moduleName = StringUtils.getUrlModule(path);
        String tempToken = TokenUtil.createTempToken(moduleName);
        requestTemplate.header(Constants.Jwt.TOKEN_HEAD,tempToken);
    }

    @Bean
    Logger.Level feignLoggerLeave(){
        return Logger.Level.FULL;
    }

}
